#!/bin/sh
#
# Updates src/third_party/wiredtiger contents
# from github.com/wiredtiger/wiredtiger branch "mongodb-3.2" or the first argument passed
#

set -o errexit

REMOTEBRANCH="${1:-develop}"
LOCALBRANCH=$(git symbolic-ref HEAD)

# Ensure working directory is TOPLEVEL/src/third_party
STARTPWD=$(pwd)
TOPLEVEL=$(git rev-parse --show-toplevel)
cd ${TOPLEVEL}/src/third_party

# Write file according to "Content-Disposition: attachment" header. Example:
# Content-Disposition: attachment; filename=wiredtiger-wiredtiger-2.4.0-109-ge5aec44.tar.gz

rm -f wiredtiger-wiredtiger-*.tar.gz
curl -OJL https://api.github.com/repos/wiredtiger/wiredtiger/tarball/${REMOTEBRANCH}

TARBALL=$(echo wiredtiger-wiredtiger-*.tar.gz)
test -f "$TARBALL"

# Delete everything in wiredtiger dir, then selectively undelete

mkdir -p wiredtiger
(cd wiredtiger;
 rm -rf *;
 git checkout -- .gitignore 'SCons*';
 git checkout -- 'build_*/wiredtiger_config.h')

# Tar options:
# - Exclude subdirs "api", "test", "src/docs"
# - Strip 'wiredtiger-wiredtiger-e5aec44/' prefix after exclude applied
# - Change to dir "wiredtiger" before extracting

tar -x --strip-components 1 \
    --exclude '*/api' --exclude '*/dist/package' --exclude '*/examples' \
    --exclude '*/src/docs' --exclude '*/test' \
    --exclude '*/tools/wtperf_stats' \
    --exclude '*/tools/wtstats/template' \
    -C wiredtiger -f ${TARBALL}

# Add wiredtiger as a remote called 'wt'; ignore errors if it already
# exists -- fetch will catch any problems
echo "Fetching latest wiredtiger code; this may take a while..."
git remote add wt git@github.com:wiredtiger/wiredtiger.git > /dev/null 2>&1 || true
git fetch wt

# Build a changelog for the revision history
# - get the githash for this drop from the tarball name
newhash="`echo $TARBALL | sed -e 's/.*-g\([0-9a-f]*\)\..*/\1/'`"
[ -z "$newhash" ] && echo "Can't find hash for this drop" && exit 1

# - find the githash from the previous drop from the summary
oldhash="`git log --format=%s | grep 'Import wiredtiger' | head -n 1 | sed -e's/.*-g\([0-9a-f]*\)\..*/\1/g'`"
[ -z "$oldhash" ] && echo "Can't find hash for previous drop" && exit 1

# - build the changelog; unfortunately the format is branch-dependant
case ${REMOTEBRANCH} in
    "mongodb-3.0") format="--format=\"%h  %s\"" ;;
    "mongodb-3.2") format="--grep=\"Merge pull\" --format=\"%h  %b\"" ;;
    *) echo "${REMOTEBRANCH} is not supported; bailing out" ; exit 1 ;;
esac
gitlog="git log $format ${oldhash}..${newhash} wt/${REMOTEBRANCH}"
changelog="`eval $gitlog`"

echo
git add wiredtiger
git commit -m "Import ${TARBALL} from wiredtiger branch ${REMOTEBRANCH}" \
 -m "ref: ${oldhash}..${newhash}" \
 -m "$changelog"
echo
git log -n 1

set -o errexit
cd $STARTPWD
echo "
Done applying $TARBALL to $LOCALBRANCH.
The revision history was computed as follows:

  $gitlog

Please review it before you push. You can edit it with:

  git commit --amend

"
